//
//  ShopSectionView.m
//  sensen_ios
//
//  Created by pro on 16/2/22.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ShopSectionView.h"
#import "ProductDetailsVC.h"

@interface ShopSectionView ()

@end

@implementation ShopSectionView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self _initViews];
    }
    
    return self;
}

#pragma mark -- 创建视图 --
- (void)_initViews {

    self.backgroundColor = [UIColor colorWithWhite:0.98 alpha:1];
    
    CGFloat margin = 3;
    CGFloat bigWidth = (SCREEN_WIDTH - margin * 3) / 2;
    CGFloat smallWidth = (bigWidth - margin) / 2;
    
    // —————————— 宠物馆 —————————— 类似这样的
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat width = 60;
    CGFloat height = 20;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    label.backgroundColor = self.backgroundColor;
    label.font = FONT(14);
    label.text = @"宠物馆";
    label.textColor = RED_COLOR;
    label.textAlignment = NSTextAlignmentCenter;
    label.center = CGPointMake(SCREEN_WIDTH / 2.0, label.center.y);
    [self addSubview:label];
    label.tag = 1;
    
    // 线
    width = SCREEN_WIDTH;
    height = 1;
    y = (20 - 1) / 2.0;
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    line.backgroundColor = RED_COLOR;
    [self insertSubview:line atIndex:0];
    line.tag = 2;
    
    // 图片
    // 第一张
    x = 5;
    y = label.bottom + 10;
    width = bigWidth;
    height = width;
    UIImageView *firstImgView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    firstImgView.image = [UIImage imageWithColor:[UIColor colorWithWhite:0.95 alpha:1]];
    [self addSubview:firstImgView];
    firstImgView.tag = 11;
    
    // 第二张
    x = firstImgView.right + margin;
    y = firstImgView.top;
    width = smallWidth;
    height = width;
    UIImageView *sectionImgView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    sectionImgView.image = [UIImage imageWithColor:[UIColor colorWithWhite:0.95 alpha:1]];
    [self addSubview:sectionImgView];
    sectionImgView.tag = 12;
    
    // 第三张
    x = sectionImgView.right + margin;
    UIImageView *thirdImgView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    thirdImgView.image = [UIImage imageWithColor:[UIColor colorWithWhite:0.95 alpha:1]];
    [self addSubview:thirdImgView];
    thirdImgView.tag = 13;
    
    // 第4张
    x = sectionImgView.left;
    y = sectionImgView.bottom + margin;
    UIImageView *fourthImgView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    fourthImgView.image = [UIImage imageWithColor:[UIColor colorWithWhite:0.95 alpha:1]];
    [self addSubview:fourthImgView];
    fourthImgView.tag = 14;
    
    // 第5张
    x = fourthImgView.right + margin;
    UIImageView *fivethImgView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    fivethImgView.image = [UIImage imageWithColor:[UIColor colorWithWhite:0.95 alpha:1]];
    [self addSubview:fivethImgView];
    fivethImgView.tag = 15;
    
    for (UIImageView *imgView in self.subviews) {
        imgView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_tapAction:)];
        [imgView addGestureRecognizer:tap];
    }
    
}

#pragma mark - 点击
- (void)_tapAction:(UITapGestureRecognizer *)tap {
    /*
     8个馆，tag 1000 - 1007
     每个馆中5个商品 tag 11 - 15
     */
    NSLog(@"%@", _modelAry);
    if (_modelAry == nil) {
        return;
    }
    NSInteger index = tap.view.tag - 11;
    ProductDetailsVC *vc = [[ProductDetailsVC alloc] init];
    if (index < _modelAry.count) {
        ProductModel *model = _modelAry[index];
        vc.pid = model.p_id;
        [self.viewController.navigationController pushViewController:vc animated:YES];
    }
    
}

#pragma mark - 重写set
- (void)setModelAry:(NSMutableArray *)modelAry {
    _modelAry = modelAry;
    
    NSMutableArray *imgAry = [[NSMutableArray alloc] init];
    for (ProductModel *model in modelAry) {
        [imgAry addObject:model.img];
    }
    
    for (int i = 0; i < imgAry.count; i ++) {
        UIImageView *imgView = [self viewWithTag:11 + i];
        [imgView setImageURLStr:imgAry[i] placeholder:DEFAULT_IMAGE];
    }
}
- (void)setImgAry:(NSMutableArray *)imgAry {
    
    NSInteger index = 0;
    // 数量不足5个要添加满
    if (imgAry.count < 5) {
        index = imgAry.count;
        while (index < 5) {
            [imgAry addObject:@""];
            index ++;
        }
    }
   
    
    for (int i = 0; i < imgAry.count; i ++) {
        UIImageView *imgView = [self viewWithTag:11 + i];
        [imgView setImageURLStr:imgAry[i] placeholder:DEFAULT_IMAGE];
    }
}
// 设置title
- (void)setTitle:(NSString *)title {
    UILabel *label = (UILabel *)[self viewWithTag:1];
    label.text = title;
}
// 设置字体和线条颜色
- (void)setTitleColor:(UIColor *)titleColor {
    UILabel *label = (UILabel *)[self viewWithTag:1];
    label.textColor = titleColor;
}
- (void)setLineColor:(UIColor *)lineColor {
    UIView *line = [self viewWithTag:2];
    line.backgroundColor = lineColor;
}


@end
